/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skytestarmor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import ru.jSkyRoom.bridge.major.FormListBridge;
import ru.jSkyRoom.factory.BridgeFactory;
import ru.jSkyRoom.factory.FinderByEdidFactory;
import ru.jSkyRoom.factory.FinderByFormIdFactory;
import ru.jSkyRoom.finder.FinderByEdidFacade;
import ru.jSkyRoom.interfaces.JSkyMod;
import ru.jSkyRoom.interfaces.major.Enchantment;
import ru.jSkyRoom.interfaces.major.FormList;
import ru.jSkyRoom.interfaces.major.Keyword;
import ru.jSkyRoom.selector.SelectorFacade;
import skyproc.ENCH;
import skyproc.FLST;
import skyproc.FormID;
import skyproc.GRUP;
import skyproc.KYWD;
import skyproc.Mod;

/**
 *
 * @author alx
 */
public class EnbhantmentsBank {

    private Map<Keyword, Map<String, Enchantment>> keywordEnchantments = new HashMap<>();
    private Map<Keyword, ArrayList<FormList>> keywordFormlist = new HashMap<>();
    private Map<FormList, ArrayList<Keyword>> formlistKeyword = new HashMap<>();
    private Map<Keyword, ArrayList<Enchantment>> keywordBaseEnch = new HashMap<>();
    private Map<Enchantment, ArrayList<Keyword>> baseEnchKeyword = new HashMap<>();
    private JSkyMod kepper_Radiant_LB_esp;

    public EnbhantmentsBank(JSkyMod libMods, LeveledOutfits leveledOutfits) {
        super();
        /*
         leveledOutfits.subSetNodes = readSubSets(libMods, leveledOutfits);
         ArrayList<RecordNameLvlEntituList> topOutfitNodes = leveledOutfits.readOutfits();
         leveledOutfits.createLeveledStructure(topOutfitNodes, libMods);
         */
    }

    public Map<String, Enchantment> getEnchListByKeyword(Keyword keyword) {
        return keywordEnchantments.get(keyword);
    }

    public EnbhantmentsBank(JSkyMod kepper_Radiant_LB_esp) {
        this.kepper_Radiant_LB_esp = kepper_Radiant_LB_esp;
        fullKeywordFormlist();
        fullkeywordBaseEnch();
        fullBaseEnchantmentsKeyword();
    }

    public EnbhantmentsBank(JSkyMod kepper_Radiant_LB_esp, Boolean hhj) {
        this.kepper_Radiant_LB_esp = kepper_Radiant_LB_esp;



    }

    private void fullKeywordFormlist() {
        Collection<Keyword> allKeywords = getSelectorKRLB().selectKeywords();

        for (FormList formlist : getSelectorKRLB().selectFormLists()) {
            String tem = formlist.getEDID();
            if (formlist.getEDID().startsWith("Enchantment")) {
                ArrayList<Keyword> listOfKeywords = new ArrayList<>();

                Collection<FormID> formlistIDEntries = ((FormListBridge) formlist).getRawObject().getFormIDEntries();
                Collection<Keyword> formListKeyword = FinderByFormIdFactory.create().createKeywordFinder().find(formlistIDEntries);

                for (Keyword keyword : formListKeyword) {
                    listOfKeywords.add(keyword);
                    if (keywordFormlist.containsKey(keyword)) {
                        keywordFormlist.get(keyword).add(formlist);
                    } else {
                        ArrayList<FormList> ListOfFormList = new ArrayList<>();
                        ListOfFormList.add(formlist);
                        keywordFormlist.put(keyword, ListOfFormList);
                    }

                }
                formlistKeyword.put(formlist, listOfKeywords);
            }
        }
    }

    private void fullkeywordBaseEnch() {
        ArrayList<Keyword> listOfFormLists;
        for (Enchantment ench : getSelectorKRLB().selectEnchantments()) {
            if (ench.getEDID().endsWith("Base") && !ench.getEDID().startsWith("EnchWeapon")) {
                listOfFormLists = formlistKeyword.get(ench.getWornRestrictions());
                //baseEnchKeyword
                if (listOfFormLists != null) {
                    ArrayList<Keyword> listOfKeywords = new ArrayList<>();
                    for (Keyword keyword : listOfFormLists) {
                        listOfKeywords.add(keyword);
                        if (keywordBaseEnch.containsKey(keyword)) {
                            keywordBaseEnch.get(keyword).add(ench);
                        } else {
                            ArrayList<Enchantment> listOfBaseEnchantments = new ArrayList<>();
                            listOfBaseEnchantments.add(ench);
                            keywordBaseEnch.put(keyword, listOfBaseEnchantments);
                        }
                    }
                    baseEnchKeyword.put(ench, listOfKeywords);
                }
            }
        }
    }

    private void fullkeywordEnchantments() {
        for (Enchantment ench : getSelectorKRLB().selectEnchantments()) {
            String enchName = ench.getEDID();
            if (enchName.startsWith("EnchRobesFortify") || enchName.startsWith("EnchArmor")) {
                baseEnchKeyword.put(ench, formlistKeyword.get(ench.getWornRestrictions()));
            }

        }

    }

    private void fullBaseEnchantmentsKeyword() {
        Map<String, Enchantment> enchArmorListForm = new HashMap<>(200);
        Map<String, Enchantment> enchRobesListForm = new HashMap<>(200);
        Map<String, Enchantment>[] returnMaps = new Map[2];

        for (Enchantment ench : getSelectorKRLB().selectEnchantments()) {
            String enchName = ench.getEDID();
            if (enchName.startsWith("EnchRobesFortify") || enchName.startsWith("EnchArmor")) {
                baseEnchKeyword.put(ench, formlistKeyword.get(ench.getWornRestrictions()));
            }

        }
    }

    private void fullkeywordEnchantments2() {
        int l_EnchRobesFortify = "EnchRobesFortify".length();
        int l_EnchArmorFortify = "EnchArmorFortify".length();
        int l_EnchArmorResist = "EnchArmorResist".length();
        int l_EnchArmor = "EnchArmor".length();

        for (Enchantment ench : getSelectorKRLB().selectEnchantments()) {
            String enchName = ench.getEDID();
            if (enchName.startsWith("EnchRobesFortify") && !enchName.endsWith("Base")) {
                enchName = enchName.substring(l_EnchRobesFortify);
                putEnchRecord(ench, enchName);
            } else if (enchName.startsWith("EnchArmor") && !enchName.endsWith("Base")) {
                if (enchName.startsWith("EnchArmorFortify")) {
                    enchName = enchName.substring(l_EnchArmorFortify);
                } else if (enchName.startsWith("EnchArmorResist")) {
                    enchName = enchName.substring(l_EnchArmorResist);
                } else {
                    enchName = enchName.substring(l_EnchArmor);
                }
                putEnchRecord(ench, enchName);
            }

        }

    }

    private Map<String, Enchantment>[] getVanilaArmorRobeEnbhantments() {
        Map<String, Enchantment> enchArmorListForm = new HashMap<>(200);
        Map<String, Enchantment> enchRobesListForm = new HashMap<>(200);
        Map<String, Enchantment>[] returnMaps = new Map[2];

        int l_EnchRobesFortify = "EnchRobesFortify".length();
        int l_EnchArmorFortify = "EnchArmorFortify".length();
        int l_EnchArmorResist = "EnchArmorResist".length();
        int l_EnchArmor = "EnchArmor".length();

        for (Enchantment ench : getSelectorKRLB().selectEnchantments()) {
            String enchName = ench.getEDID();
            if (enchName.startsWith("EnchRobesFortify") && !enchName.endsWith("Base")) {
                enchName = enchName.substring(l_EnchRobesFortify);
                enchRobesListForm.put(enchName, ench);

            } else if (enchName.startsWith("EnchArmor") && !enchName.endsWith("Base")) {
                if (enchName.startsWith("EnchArmorFortify")) {
                    enchName = enchName.substring(l_EnchArmorFortify);
                } else if (enchName.startsWith("EnchArmorResist")) {
                    enchName = enchName.substring(l_EnchArmorResist);
                } else {
                    enchName = enchName.substring(l_EnchArmor);
                }
                enchArmorListForm.put(enchName, ench);
            }
        }




        return returnMaps;
    }

    private void putEnchRecord(Enchantment ench, String enchName) {
        /* for(KYWD keyword : baseEnchKeyword.get(ench.getBaseEnchantment())){
         if (keywordEnchantments.containsKey(keyword)) {
         keywordEnchantments.get(keyword).put(enchName, ench);
         }else{
         Map<String, ENCH> enchantments = new HashMap<>();
         enchantments.put(enchName, ench);
         keywordEnchantments.put(keyword.getForm(), enchantments);
         }
         }*/
    }

    private void fullKeywordEnchantmentsFromList() {
        FormList formList = FinderByEdidFacade.create().findFormList("EnchList_ArmorShield");

        Collection<FormID> formlistIDEntries = ((FormListBridge) formList).getRawObject().getFormIDEntries();
        Collection<Keyword> formListKeyword = FinderByFormIdFactory.create().createKeywordFinder().find(formlistIDEntries);

        for (Keyword keyword : formListKeyword) {
        }

        /*
         for (FLST formlist : kepper_Radiant_LB_esp.getFormLists()) {
         String tem = formlist.getEDID();
         if (formlist.getEDID().startsWith("Enchantment")) {
         ArrayList<FormID> listOfKeywords= new ArrayList<FormID>();
         for (FormID keyword : formlist.getFormIDEntries()) {
         listOfKeywords.add(keyword);
         if (keywordFormlist.containsKey(keyword)) {
         keywordFormlist.get(keyword).add(formlist.getForm());
         } else {
         ArrayList<FormID> ListOfFormList = new ArrayList<FormID>();
         ListOfFormList.add(formlist.getForm());
         keywordFormlist.put(keyword, ListOfFormList);
         }

         }
         formlistKeyword.put(formlist.getForm(), listOfKeywords);
         }
         }
         */

    }

    protected SelectorFacade getSelectorKRLB() {
        return SelectorFacade.create(kepper_Radiant_LB_esp);
    }
}
